package com.gmss.crawler.module.honor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gmss.crawler.module.honor.domain.Honor;
import com.gmss.crawler.module.honor.mapper.HonorMapper;
import com.gmss.crawler.module.honor.service.IHonorService;
import com.gmss.crawler.module.person.domain.PersonInfo;
import com.gmss.crawler.utils.CommonUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 荣誉表彰表 服务实现类
 * </p>
 *
 * @author Auto-generator
 * @since 2021-10-18
 */
@Service
public class HonorServiceImpl extends ServiceImpl<HonorMapper, Honor> implements IHonorService {


    public void saveHonor(PersonInfo personInfo, List<Honor> list) {
        for (int i = list.size() - 1; i >= 0; i--) {
            Honor honor = list.get(i);
            if (StringUtils.isBlank(honor.getPrizeName())) {
                list.remove(i);
            } else {
                honor.setPersonId(personInfo.getId());
            }
        }
        LambdaQueryWrapper<Honor> honorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        honorLambdaQueryWrapper.eq(Honor::getPersonId, personInfo.getId());
        int count = count(honorLambdaQueryWrapper);
        if (count > 0) {
            remove(honorLambdaQueryWrapper);
        }
        saveBatch(list);
    }

    @Override
    public void queryHonorSave(PersonInfo personInfo, String data, Integer type) {
        Honor honor = new Honor().builder().personId(personInfo.getId()).type(type).prizeName(data).build();
        LambdaQueryWrapper<Honor> wrapper = new LambdaQueryWrapper();
        wrapper.eq(Honor::getPersonId, personInfo.getId());
//        wrapper.eq(Honor::getPrizeName, data);
//        List<Honor> resumeList = list(wrapper);
//        if (CollectionUtils.isEmpty(resumeList))
        if (count(wrapper) <= 0)
            save(honor);
    }

    /**
     * 查询personId在Honor（科研奖项/荣誉表彰）表中是否有数据，如果没有插入，有则不处理。
     *
     * @param personInfo
     * @param list
     */
    @Override
    public void saveHonorByPersonId(PersonInfo personInfo, List<Honor> list) {
        if (CollectionUtils.isNotEmpty(list)) {
            LambdaQueryWrapper<Honor> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Honor::getPersonId, personInfo.getId());
            int count = count(queryWrapper);
            if (count <= 0) {
                for (Honor queryData : list) {
                    queryData.setPersonId(personInfo.getId());
                    save(queryData);
                }
            }
        }
    }
}
